﻿using FCBLL;
using FCModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileConverter.Bas
{
    public partial class AddFileType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                QueryTestTypeByTestType();
                QueryTestparamByTestType();
            }
        }
        /// <summary>
        /// 绑定 bas_TestType
        /// </summary>
        protected void Bindbas_TestType()
        {
            GV_bas_TestType.DataSource = new BaseRepositoryBLL<bas_TestType>().LoadAllEntities().ToList<bas_TestType>();
            GV_bas_TestType.DataBind();
        }
        /// <summary>
        /// 绑定 bas_Testparam
        /// </summary>
        protected void Bindbas_Testparam()
        {
            GV_bas_Testparam.DataSource = new BaseRepositoryBLL<bas_Testparam>().LoadAllEntities().ToList<bas_Testparam>();
            GV_bas_Testparam.DataBind();
        }

        protected void QueryTestparamByTestType()
        {
            if (String.IsNullOrWhiteSpace(TestType1.Text))
                Bindbas_Testparam();
            else
            {
                GV_bas_Testparam.DataSource = new BaseRepositoryBLL<bas_Testparam>()
                    .LoadEntities(p => p.testType.Equals(TestType1.Text.Trim(), StringComparison.OrdinalIgnoreCase)).ToList<bas_Testparam>();
                GV_bas_Testparam.DataBind();
            }
        }

        protected void QueryTestTypeByTestType()
        {
            if (String.IsNullOrWhiteSpace(TestType.Text))
                Bindbas_TestType();
            else
            {
                GV_bas_TestType.DataSource = new BaseRepositoryBLL<bas_TestType>()
                    .LoadEntities(p => p.testType.Equals(TestType.Text.Trim(), StringComparison.OrdinalIgnoreCase)).ToList<bas_TestType>();
                GV_bas_TestType.DataBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryTestTypeByTestType();
        }


        /// <summary>
        /// 新增 bas_TestType
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddTestType_Click(object sender, EventArgs e)
        {
            var btt = new bas_TestType();
            btt.testType = TestType.Text;
            btt.testTypeDesc = TestTypeDesc.Text;
            btt.type = Type.Text;
            btt.dataFormatType = DataFormatType.Text;
            btt.dataFieldIndex = DataFieldIndex.Text;
            btt.HeadIndex = Convert.ToInt32(headIndex.Text);
            btt.ValueIndex = Convert.ToInt32(ValueIndex.Text);
            btt.isAbs = isAbs.Checked;
            new BaseRepositoryBLL<bas_TestType>().AddEntities(btt);
            GV_bas_TestType.EditIndex = -1;
            Bindbas_TestType();
        }
        /// <summary>
        /// 新增 bas_Testparam
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddTestparam_Click(object sender, EventArgs e)
        {
            var btp = new bas_Testparam();
            btp.testType = TestType1.Text;
            btp.testParam = TestParam.Text;
            btp.seqNo = Convert.ToInt32(seqNo.Text);
            btp.longName = LongName.Text;
            btp.Express = exp.Text;
            new BaseRepositoryBLL<bas_Testparam>().AddEntities(btp);
            GV_bas_TestType.EditIndex = -1;
            Bindbas_TestType();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_bas_TestType.EditIndex = -1;
            Bindbas_TestType();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var btt = new bas_TestType();
            btt.testType = GV_bas_TestType.DataKeys[e.RowIndex]["testType"].ToString().Trim();
            new BaseRepositoryBLL<bas_TestType>().DeleteEntities(btt);
            GV_bas_TestType.EditIndex = -1;
            Bindbas_TestType();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var btt = new bas_TestType(); 
            btt.testType = GV_bas_TestType.Rows[e.RowIndex].Cells[0].Text.Trim();
            btt.type = ((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
            btt.testTypeDesc = ((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
            btt.dataFormatType = ((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
            btt.dataFieldIndex = ((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[4].FindControl("TBqueryType")).Text.Trim();
            btt.isAbs = ((CheckBox)GV_bas_TestType.Rows[e.RowIndex].Cells[5].Controls[0]).Checked;
            btt.HeadIndex=Convert.ToInt32(((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim());
            btt.ValueIndex = Convert.ToInt32(((TextBox)GV_bas_TestType.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim());
            new BaseRepositoryBLL<bas_TestType>().UpdateEntities(btt);
            GV_bas_TestType.EditIndex = -1;
            Bindbas_TestType();
        }

        /// <summary>
        /// 查找方式更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DDLqueryType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DDLqueryType = sender as DropDownList;
            GridViewRow gvr = (GridViewRow)(sender as DropDownList).NamingContainer;
            TextBox TBqueryType = gvr.FindControl("TBqueryType") as TextBox;
            TBqueryType.Text = DDLqueryType.SelectedValue;

        }


        
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var btp = new bas_Testparam();
            btp.testType = GV_bas_Testparam.DataKeys[e.RowIndex]["testType"].ToString().Trim();
            btp.testParam = GV_bas_Testparam.DataKeys[e.RowIndex]["testParam"].ToString().Trim();
            btp.seqNo = Convert.ToInt32(GV_bas_Testparam.DataKeys[e.RowIndex]["seqNo"].ToString().Trim());
            new BaseRepositoryBLL<bas_Testparam>().DeleteEntities(btp);
            GV_bas_Testparam.EditIndex = -1;
            Bindbas_Testparam();
        }
        /// <summary>
        /// 批量导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnImportTestParam_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName == "" || FileUpload1.PostedFile.InputStream.Length < 1)
            {
                Alert("没有指定文件.");
            }
            else
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                if (fileExt != ".csv")
                {
                    Alert("请使用指定csv文件");
                    return;
                }
                try
                {
                    byte[] byFile = new byte[FileUpload1.PostedFile.ContentLength];
                    FileUpload1.PostedFile.InputStream.Read(byFile, 0, FileUpload1.PostedFile.ContentLength);
                    FileUpload1.PostedFile.InputStream.Close();
                    string strFile = System.Text.Encoding.ASCII.GetString(byFile);
                    string[] arrFile = strFile.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    if (arrFile.Length == 0) return;
                    foreach (string str in arrFile)
                    {
                        if (str.StartsWith("testType", StringComparison.OrdinalIgnoreCase)) continue;
                        string[] sptmp = str.Split(',');
                        var btp = new bas_Testparam();
                        btp.testType = sptmp[0].Trim();
                        btp.testParam = sptmp[1].Trim();
                        btp.seqNo = Convert.ToInt32(sptmp[2].Trim());
                        btp.longName = sptmp[3].Trim();
                        btp.Express = sptmp[4].Trim();
                        btp.minValue = null;
                        btp.maxValue = null;
                        btp.valueDesc = null;
                        btp.valueType = null;
                        btp.masterField = null;
                        new BaseRepositoryBLL<bas_Testparam>().AddEntities(btp);
                    }
                    GV_bas_Testparam.EditIndex = -1;
                    Bindbas_Testparam();
                }
                catch (Exception err)
                {
                    Alert(err.Message);
                }
            }
        }

        public void Alert(string description)
        {
            if (description != null)
            {
                string scriptString = "<script language=JavaScript>";
                scriptString += "alert('" + description + "');";
                scriptString += " </script>";
                if (!Page.IsClientScriptBlockRegistered("clientScript"))
                    Page.RegisterClientScriptBlock("clientScript", scriptString);
            }
            else
            {
                Alert("描述信息为空！");
            }
        }

        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var btp = new bas_Testparam();
            btp.testType = GV_bas_Testparam.Rows[e.RowIndex].Cells[0].Text.Trim();
            btp.testParam = GV_bas_Testparam.Rows[e.RowIndex].Cells[1].Text.Trim();
            btp.seqNo = Convert.ToInt32(GV_bas_Testparam.Rows[e.RowIndex].Cells[2].Text.Trim());

            btp.longName = ((TextBox)GV_bas_Testparam.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
            btp.Express = ((TextBox)GV_bas_Testparam.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();

            new BaseRepositoryBLL<bas_Testparam>().UpdateEntities(btp);
            GV_bas_Testparam.EditIndex = -1;
            Bindbas_Testparam();
        }

        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void GV_bas_TestType_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_bas_TestType.EditIndex = e.NewEditIndex;
            Bindbas_TestType();
        }

        protected void GV_bas_Testparam_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_bas_Testparam.EditIndex = e.NewEditIndex;
            Bindbas_Testparam();
        }

        protected void GV_bas_Testparam_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_bas_Testparam.EditIndex = -1;
            Bindbas_Testparam();
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_bas_Testparam.PageIndex = e.NewPageIndex;
        }

        protected void GV_bas_Testparam_PageIndexChanged(object sender, EventArgs e)
        {
            QueryTestparamByTestType();
        }

        protected void GV_bas_TestType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_bas_TestType.PageIndex = e.NewPageIndex;
        }

        protected void GV_bas_TestType_PageIndexChanged(object sender, EventArgs e)
        {
            QueryTestTypeByTestType();
        }

        protected void QueryTestParam_Click(object sender, EventArgs e)
        {
            QueryTestparamByTestType();
        }

        protected void GV_bas_TestType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (((LinkButton)e.Row.Cells[8].Controls[2]).Text.Equals("删除"))
                {
                    ((LinkButton)e.Row.Cells[8].Controls[2]).Attributes.Add("onclick",
                        string.Format("javascript:return confirm('您是要删除文件类型为" + e.Row.Cells[0].Text + "的数据吗？')"));
                }
            }

            if ((DropDownList)e.Row.FindControl("DDLqueryType") != null)
            {
                DropDownList DDLqueryType = (DropDownList)e.Row.FindControl("DDLqueryType");
                DDLqueryType.SelectedValue = ((TextBox)e.Row.FindControl("TBqueryType")).Text;
            }
        }

        protected void GV_bas_Testparam_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (((LinkButton)e.Row.Cells[5].Controls[2]).Text.Equals("删除"))
                {
                    ((LinkButton)e.Row.Cells[5].Controls[2]).Attributes.Add("onclick",
                        string.Format("javascript:return confirm('您是要删除文件类型为" + e.Row.Cells[0].Text + "的数据吗？')"));
                }
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            new BaseRepositoryBLL<bas_Testparam>().DeleteEntities(p => p.testType.Equals(TestType1.Text.Trim(), StringComparison.OrdinalIgnoreCase));
            QueryTestparamByTestType();
        }
        
    }
}